package com.moreemrecife.ui;

import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 * Helper class to build response messages
 * @author Adriano
 */
public class MessageHelper {

    static ResourceBundle msgs;

    static {
        msgs = ResourceBundle.getBundle("Messages");
    }

    /**
     * Build a "Record {0} Updated" Message
     * @param id
     * @return
     */
    public static void saveSuccess(int id) {
        FacesContext fc = FacesContext.getCurrentInstance();
        String format = msgs.getString("savedOkGreeting");
        String message = String.format(format, id);
        fc.addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_INFO, message == null ? "Record Updated"
                : message, ""));
    }

    /**
     * Report a condition in the system
     * @param msgId Message Id from the resource bundle
     */
    public static void reportError(String msgId) {
        FacesContext fc = FacesContext.getCurrentInstance();
        String message = msgs.getString(msgId);
        String exceptionInLog = msgs.getString("exceptionInLog");
        fc.addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, message == null ? "Error" : message,
                exceptionInLog));
    }

    public static void reportError(String msgId, Object[] args) {
        FacesContext fc = FacesContext.getCurrentInstance();
        String format = msgs.getString(msgId);
        String exceptionInLog = msgs.getString("exceptionInLog");
        String message = String.format(format, args);
        fc.addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_ERROR, message == null ? "Error" : message,
                exceptionInLog));
    }

}
